package com.liang.controller;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.swing.JOptionPane;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liang.pojo.CarItem;
import com.liang.pojo.Gorillaceo;
import com.liang.pojo.Store;
import com.liang.service.CarItemService;
import com.liang.service.StoreService;

@Controller
public class CarItemController {
	@Autowired
	CarItemService carItemService;
	@Autowired
	StoreService storeService;
	
	@RequestMapping("/car/car")
	public String toCar() {
		return "car/car";
	}
	
	@ResponseBody
	@RequestMapping("/car/selectAllCarItemByUid")
	public Object selectAllCarItem(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,HttpServletRequest request) {
		// 这个必须放在查询条件的前面，最好第一行
		PageHelper.startPage(pageNum, 12);
		int uid=(int) request.getSession().getAttribute("uid");
		
		List<CarItem> allCarItem = carItemService.selectAllCarItemByUid(uid);
		/*
		 * for (CarItem carItem : allCarItem) { Store store =
		 * storeService.selectStoreById(carItem.getId()); }
		 */
		PageInfo<CarItem> pageInfo = new PageInfo<CarItem>(allCarItem);
		System.out.println(pageInfo);
		System.out.println(pageInfo.getList());
		for (CarItem carItem : pageInfo.getList()) {
			System.out.println(carItem);
		}
		return pageInfo;
	}

	@RequestMapping("/car/insertCarItem")
	@ResponseBody
	public String insertCarItem(Store store,HttpServletRequest request,String count,String subtotal) {//添加到购物车
		/* 1,商品信息加入购物车
		 * 2，数据库查询购物车有商品的id就更新这个购物车的number和更新小计，否则插入购车新购物车
		 * 3.用户一点击添加购物车
		 * 
		 * */
		System.out.println(store);
		Map<String,Object> map=new LinkedHashMap<String,Object>();
		int uid = (int) request.getSession().getAttribute("uid");
		CarItem carItem = carItemService.selectCarItemBysid(store.getSid(),uid);
		map.put("sid",store.getSid());map.put("uid", uid);
		if(carItem==null) {
			map.put("count",Integer.parseInt(count));
			map.put("subtotal", Double.parseDouble(subtotal));
			carItemService.insertCarItem(map);
			request.getSession().setAttribute("car", (int)request.getSession().getAttribute("car")+1);
		}else {
			if(carItem.getCount()+Integer.parseInt(count)>99) {
				map.put("count", 99);
				map.put("subtotal", 99*store.getPrice());
			}else {
				map.put("count",carItem.getCount()+Integer.parseInt(count));
				map.put("subtotal", carItem.getSubtotal()+Double.parseDouble(subtotal));
			}
			carItemService.updateCarItem(map);
		}
		return "ok";
	}
	@ResponseBody
	@RequestMapping("/car/updateCarItem")
	public Object updateCarItem(@RequestParam("sid")int id,@RequestParam("count")int count,@RequestParam("price")double price,
			@RequestParam("key")String key,HttpServletRequest request) {
		System.out.println(key);
		int uid = (int) request.getSession().getAttribute("uid");System.out.println(uid);
		Map<String,Object> map = new HashMap<String,Object>();
		if(key.equals("sub")) {
			if(count!=1) {
				map.put("sid", id);map.put("count", count-1);map.put("subtotal", (count-1)*price);map.put("uid", uid);
				carItemService.updateCarItem(map);
				return 0;
			}else {
				return 1;
			}
		}
		if(key.equals("add")) {
			if(count==99) {
				map.put("sid", id);map.put("count", count);map.put("subtotal", count*price);map.put("uid", uid);
			}else {
				map.put("sid", id);map.put("count", count+1);map.put("subtotal", (count+1)*price);map.put("uid", uid);
			}
			carItemService.updateCarItem(map);
			return 2;
		}
		if(key.equals("shiqu")) {
			if(count>99) {
				map.put("sid", id);map.put("count", 99);map.put("subtotal", 99*price);map.put("uid", uid);
			}else if(count<1){
				map.put("sid", id);map.put("count", 1);map.put("subtotal", price);map.put("uid", uid);
			}else {
				map.put("sid", id);map.put("count", count);map.put("subtotal", count*price);map.put("uid", uid);
			}
			carItemService.updateCarItem(map);
		}
		return 3;
	}
	@RequestMapping("/car/deleteBatch")
	@ResponseBody
	public int deleteBatch(Gorillaceo gorillaceo,HttpServletRequest request) {
		int res = JOptionPane.showConfirmDialog(null, "是否继续删除", "是否继续", JOptionPane.YES_NO_OPTION);
		int i = 0;
		if (res == JOptionPane.YES_OPTION) {
			int uid = (int) request.getSession().getAttribute("uid");System.out.println(uid);
			gorillaceo.setUid(uid);
			i = carItemService.deleteCarItem(gorillaceo); // 点击“是”后执行这个代码块
			request.getSession().setAttribute("car", (int)request.getSession().getAttribute("car")-i);
		} else {
			i = -1; // 点击“否”后执行这个代码块 }
		}
		return i;
	}
	@RequestMapping("/car/deleteBatch1")
	@ResponseBody
	public int deleteBatch1(Gorillaceo gorillaceo,HttpServletRequest request) {
			int uid = (int) request.getSession().getAttribute("uid");
			gorillaceo.setUid(uid);
			int i = carItemService.deleteCarItem(gorillaceo); 
			request.getSession().setAttribute("car", (int)request.getSession().getAttribute("car")-i);
		return i;
	}
}
